package com.jwsoft.manager.core.integration.bigScreen.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bifang.engine.assistant.core.annotation.ApiService;
import com.bifang.module.base.common.vo.common.CommonVO;
import com.bifang.module.base.core.integration.CommonIntegration;
import com.jwsoft.manager.common.vo.bigScreen.analysisZbXq.SchoolInfoQueryVO;
import com.jwsoft.manager.common.vo.bigScreen.analysisZbXq.SchoolInfoVO;
import com.jwsoft.manager.core.dao.model.EduSjtj03;
import com.jwsoft.manager.core.dao.service.EduSjtj03Service;
import com.jwsoft.manager.core.integration.bigScreen.AnalysisZbXqIntegration;
import com.jwsoft.manager.core.util.EduHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.Map;

@ApiService(funcCode = "AnalysisZbXq", title = "学区分析接口")
public class AnalysisZbXqIntegrationImpl implements AnalysisZbXqIntegration {
    @Autowired
    EduSjtj03Service eduSjtj03Service;
    @Autowired
    EduHelper eduHelper;
    @Autowired
    CommonIntegration commonIntegration;
    @Override
    public SchoolInfoVO getSchoolInfo(SchoolInfoQueryVO vo) {
        if(ObjectUtils.isEmpty(vo.getYear())){
            vo.setYear(eduHelper.thisYear());
        }
        if(ObjectUtils.isEmpty(vo.getAreaCode())){
            CommonVO commonVO=new CommonVO();
            commonVO.setParamKey("area_code");
            String areaCode=commonIntegration.getValueByKey(commonVO);
            vo.setAreaCode(areaCode);
        }
        SchoolInfoVO schoolInfoVO=new SchoolInfoVO();
        //学校总数
        QueryWrapper<EduSjtj03> schoolCountWrapper=new QueryWrapper<>();
        schoolCountWrapper.select("sum(target_num) as schoolCount").eq("school_category",vo.getSchoolCategory()).eq("year",vo.getYear()).eq("Area_Code",vo.getAreaCode()).eq("Target_Code","Y0068");
        Map<String,Object> schoolCountMap=eduSjtj03Service.getMap(schoolCountWrapper);
        if(!ObjectUtils.isEmpty(schoolCountMap)){
            BigDecimal bigDecimal= (BigDecimal) schoolCountMap.get("schoolCount");
            schoolInfoVO.setSchoolCount(bigDecimal.intValue());
        }else{
            schoolInfoVO.setSchoolCount(0);
        }
        //学校同比
        QueryWrapper<EduSjtj03> schoolIncreaseCountWrapper=new QueryWrapper<>();
        schoolIncreaseCountWrapper.select("sum(target_num) as schoolIncreaseCount").eq("school_category",vo.getSchoolCategory()).eq("year",vo.getYear()).eq("Area_Code",vo.getAreaCode()).eq("Target_Code","Y0069");
        Map<String,Object> schoolIncreaseCountMap=eduSjtj03Service.getMap(schoolIncreaseCountWrapper);
        if(!ObjectUtils.isEmpty(schoolIncreaseCountMap)){
            BigDecimal bigDecimal= (BigDecimal) schoolIncreaseCountMap.get("schoolIncreaseCount");
            schoolInfoVO.setSchoolIncreaseCount(bigDecimal.intValue());
        }else{
            schoolInfoVO.setSchoolIncreaseCount(0);
        }
        //民办学校
        QueryWrapper<EduSjtj03> schoolMbCountWrapper=new QueryWrapper<>();
        schoolMbCountWrapper.select("sum(target_num) as schoolMbCount").eq("school_category",vo.getSchoolCategory()).eq("year",vo.getYear()).eq("Area_Code",vo.getAreaCode()).eq("Target_Code","Y0070");
        Map<String,Object> schoolMbCountMap=eduSjtj03Service.getMap(schoolMbCountWrapper);
        if(!ObjectUtils.isEmpty(schoolMbCountMap)){
            BigDecimal bigDecimal= (BigDecimal) schoolMbCountMap.get("schoolMbCount");
            schoolInfoVO.setSchoolMbCount(bigDecimal.intValue());
        }else{
            schoolInfoVO.setSchoolMbCount(0);
        }
        //公办学校
        QueryWrapper<EduSjtj03> schoolGbCountWrapper=new QueryWrapper<>();
        schoolGbCountWrapper.select("sum(target_num) as schoolGbCount").eq("school_category",vo.getSchoolCategory()).eq("year",vo.getYear()).eq("Area_Code",vo.getAreaCode()).eq("Target_Code","Y0071");
        Map<String,Object> schoolGbCountMap=eduSjtj03Service.getMap(schoolGbCountWrapper);
        if(!ObjectUtils.isEmpty(schoolGbCountMap)){
            BigDecimal bigDecimal= (BigDecimal) schoolGbCountMap.get("schoolGbCount");
            schoolInfoVO.setSchoolGbCount(bigDecimal.intValue());
        }else{
            schoolInfoVO.setSchoolGbCount(0);
        }
        //学区地址个数
        QueryWrapper<EduSjtj03> schoolDistrictWrapper=new QueryWrapper<>();
        schoolDistrictWrapper.select("sum(target_num) as schoolDistrict").eq("school_category",vo.getSchoolCategory()).eq("year",vo.getYear()).eq("Area_Code",vo.getAreaCode()).eq("Target_Code","Y0072");
        Map<String,Object> schoolDistrictMap=eduSjtj03Service.getMap(schoolDistrictWrapper);
        if(!ObjectUtils.isEmpty(schoolDistrictMap)){
            BigDecimal bigDecimal= (BigDecimal) schoolDistrictMap.get("schoolDistrict");
            schoolInfoVO.setSchoolDistrict(bigDecimal.intValue());
        }else{
            schoolInfoVO.setSchoolDistrict(0);
        }
        //计划招生数量
        QueryWrapper<EduSjtj03> studentJhCountWrapper=new QueryWrapper<>();
        studentJhCountWrapper.select("sum(target_num) as studentJhCount").eq("school_category",vo.getSchoolCategory()).eq("year",vo.getYear()).eq("Area_Code",vo.getAreaCode()).eq("Target_Code","Y0073");
        Map<String,Object> studentJhCountMap=eduSjtj03Service.getMap(studentJhCountWrapper);
        if(!ObjectUtils.isEmpty(studentJhCountMap)){
            BigDecimal bigDecimal= (BigDecimal) studentJhCountMap.get("studentJhCount");
            schoolInfoVO.setStudentJhCount(bigDecimal.intValue());
        }else{
            schoolInfoVO.setStudentJhCount(0);
        }
        //当前适龄人员
        QueryWrapper<EduSjtj03> studentSlCountWrapper=new QueryWrapper<>();
        studentSlCountWrapper.select("sum(target_num) as studentSlCount").eq("school_category",vo.getSchoolCategory()).eq("year",vo.getYear()).eq("Area_Code",vo.getAreaCode()).eq("Target_Code","Y0074");
        Map<String,Object> studentSlCountMap=eduSjtj03Service.getMap(studentSlCountWrapper);
        if(!ObjectUtils.isEmpty(studentSlCountMap)){
            BigDecimal bigDecimal= (BigDecimal) studentSlCountMap.get("studentSlCount");
            schoolInfoVO.setStudentSlCount(bigDecimal.intValue());
        }else{
            schoolInfoVO.setStudentSlCount(0);
        }
        //本地户籍
        QueryWrapper<EduSjtj03> registerWrapper=new QueryWrapper<>();
        registerWrapper.select("sum(target_num) as register").eq("school_category",vo.getSchoolCategory()).eq("year",vo.getYear()).eq("Area_Code",vo.getAreaCode()).eq("Target_Code","Y0075");
        Map<String,Object>registerMap=eduSjtj03Service.getMap(registerWrapper);
        if(!ObjectUtils.isEmpty(registerMap)){
            BigDecimal bigDecimal= (BigDecimal) registerMap.get("register");
            schoolInfoVO.setRegister(bigDecimal.intValue());
        }else{
            schoolInfoVO.setRegister(0);
        }
        //外地户籍
        QueryWrapper<EduSjtj03> domicileAbroadWrapper=new QueryWrapper<>();
        domicileAbroadWrapper.select("sum(target_num) as domicileAbroad").eq("school_category",vo.getSchoolCategory()).eq("year",vo.getYear()).eq("Area_Code",vo.getAreaCode()).eq("Target_Code","Y0076");
        Map<String,Object>domicileAbroadMap=eduSjtj03Service.getMap(domicileAbroadWrapper);
        if(!ObjectUtils.isEmpty(domicileAbroadMap)){
            BigDecimal bigDecimal= (BigDecimal) domicileAbroadMap.get("domicileAbroad");
            schoolInfoVO.setDomicileAbroad(bigDecimal.intValue());
        }else{
            schoolInfoVO.setDomicileAbroad(0);
        }
        return schoolInfoVO;
    }
}
